import $ from 'jQuery';
import URI from 'URI';
$(function () {
    var EL = '.member-info-page-hook2';
    let $vue = new Vue({
        el: EL,
        data: {
            baseUrl: STATIC_SERVER,
            iconUrlFile: '',
            info: {
                userId:1,
                userName:'',
                gender:1,
                mobile:'00000000000',
                email:'',
                iconUrl:'',
                companyId:'',
               /* companyType:'',*/
                companyName:'',
                linkmanAddress:'',
                linkmanAddress1:'',
                linkmanPhone:'',
                roleId:'',
                roleName:''
            }
        },
        methods: {
            goBack: function () {
                M.Page.emitPrePage();
                //M.Page.refreshPage();
            },
            previewImg: function (event) {
                M.previewImg(event.target.src);
            },
            uploadImg: function (event, sign) {
                $(`#${sign}`).click();
            }
        },
        created: function () {
        },
        mounted: function () {
            let thiz = this;
            this.$nextTick(() => {
                let $createClient = $(EL);
                // 获取用户信息
                IOT.getServerData(URI.MEMBER.MODIFY.DATA, {}, function (ret) {
                    if (ret && ret.code === 200) {
                        $vue.$data.info = $.extend({}, $vue.$data.info, ret.data);
                        $vue.$data.info.linkmanAddress1 =$vue.$data.info.linkmanAddress;
                        $vue.$data.info.gender = (ret.data.gender === 1 ? '♀' : '♂');
                        ret.data.iconUrl = (ret.data.iconUrl ? ret.data.iconUrl : DEFAULT_IMG.portrait);
                        $vue.$data.iconUrlFile =ret.data.iconUrl.substr(1);
                    }
                });
                let hint = {iconUrlFile: '头像'};
                M.uploadImg($createClient, '#iconUrlFile', hint, function (filedName, src, file) {
                    thiz[filedName] = src;
                    thiz.info['iconUrlFile'] = file;
                }, function (filedName) {
                    // thiz[filedName] = DEFAULT_IMG[filedName];
                    delete thiz.info['iconUrlFile'];
                });
                $createClient.find('.member-hook').validate({
                    debug: true,
                    rules: {
                        email: {
                            email: true,
                            required: true,
                            minlength: 1,
                            maxlength: 50
                        },
                        linkmanPhone: {
                            phoneCN: true,
                            required: true,
                            maxlength: 15
                        },
                        linkmanAddress: {
                            required: true,
                            maxlength: 120
                        }
                    },
                    submitHandler: function (form) {
                        if($vue.$data.info.linkmanAddress == ""){
                            layer.alert('请按输入地址搜索列表结果选择公司地址');
                            return false;
                        }
                        let params = $vue.$data.info;
                        IOT.showOverlay('保存中，请稍等...');
                        IOT.doFileUpload2(URI.MEMBER.MODIFY.SAVE, params, function (result) {
                            IOT.hideOverlay();
                            if (result && result.code === 200) {
                                IOT.tips('保存成功！', 'success', 1000, function () {
                                    M.Page.emitPrePage();
                                   $('#memberCenter').trigger('click');
                                });
                            } else {
                                IOT.tips(result.message || '操作失败，请重试！', 'error');
                            }
                        }, function (result) {
                            IOT.hideOverlay();
                            IOT.tips(result.message || '操作失败，请重试！', 'error');
                            //console.error(result);
                        });
                        return false;
                    }
                });
            });
        }
    });
    
    var map = new BMap.Map('rental-map');
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 5);
    //输入地址事件处理 start
    var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
        {"input" :"linkmanAddress"
            ,"location" : map
        });
    $('#linkmanAddress').keyup(function(){
        $vue.$data.info.linkmanAddress = "";
    });
    ac.setInputValue($vue.$data.info.linkmanAddress1);
    ac.hide();
    ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
        var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
        
        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        $("#linkmanAddress_list").innerHTML = str;
    });
    
    var myValue;
    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        var local = new BMap.LocalSearch(map, { //智能搜索
            onSearchComplete: function () {
                var province = local.getResults().province;
                var city = local.getResults().city;
                if(myValue.indexOf(city)==-1){
                    myValue = city+myValue;
                }
                if(myValue.indexOf(province)==-1){
                    myValue = province+myValue;
                }
                $vue.$data.info.linkmanAddress1 = myValue;
                $vue.$data.info.linkmanAddress = myValue;
                $("#linkmanAddress_list").innerHTML =""
            }
        });
        local.search(myValue);
        //$("#linkmanAddress_list").innerHTML =""
    });
    
});